<template>
  <div>
    <div class="n-layout-page-header">
      <a-card :bordered="false" title="导出示例"> 根据JSON格式的数据进行导出 </a-card>
    </div>
    <a-card :bordered="false" class="mt-3 proCard">
      <BasicTable
        leftTitle="表格列表"
        leftTitleTooltip="这是一个提示"
        :columns="columns"
        :request="loadDataTable"
        row-key="id"
        ref="actionRef"
        :actionColumn="actionColumn"
        :row-selection="rowSelection"
        @select="handleSelect"
        @select-all="handleSelectAll"
      >
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="exportTabale" :disabled="!selectedRowsData.length"
              >导出选中数据</a-button
            >
            <a-button type="primary" @click="customHeader">导出自定义表头</a-button>
            <a-button type="primary" @click="defaultHeader">导出默认表头</a-button>
          </a-space>
        </template>
      </BasicTable>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref, h, unref } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { getTableList } from '@/api/table/list';
  import { columns } from '../../comp/table/basicColumns';
  import { jsonToSheetXlsx } from '@/components/Excel';
  import { Message, Modal } from '@arco-design/web-vue';
  import { DeleteOutlined, FormOutlined } from '@vicons/antd';
  import { renderIcon } from '@/utils';

  const actionRef = ref();
  const tableData = ref();
  const selectedRowsData = ref([]);

  const params = reactive({
    pageSize: 5,
    name: 'xiaoMa',
  });

  const loadDataTable = async (res) => {
    const result = await getTableList({ ...params, ...res });
    tableData.value = result.list;
    return result;
  };

  const rowSelection = {
    type: 'checkbox',
    showCheckedAll: true,
  };

  function handleSelect(rowKeys) {
    console.log(`selectedRowKeys: ${rowKeys}`);
    selectedRowsData.value = tableData.value.filter((item: any) => {
      return rowKeys.includes(item.id);
    });
  }

  function handleSelectAll(checked) {
    console.log(`allChecked: ${checked}`);
    selectedRowsData.value = checked ? tableData.value : [];
  }

  function handleDelete(record) {
    console.log(record);
    Modal.confirm({
      title: '提示',
      content: `您想删除${record.name}`,
      okText: '确认',
      cancelText: '取消',
      onOk() {
        Message.success('删除成功');
      },
      onCancel() {},
    });
  }

  function handleEdit(record) {
    console.log(record);
    Message.success('您点击了编辑按钮');
  }

  function defaultHeader() {
    // 默认Object.keys(data[0])作为header
    jsonToSheetXlsx({
      data: unref(tableData),
      filename: '使用key作为默认表头.xlsx',
    });
  }

  function customHeader() {
    jsonToSheetXlsx({
      data: unref(tableData),
      header: {
        id: 'ID',
        no: '编码',
        name: '名称',
        avatar: '头像',
        address: '地址',
        beginTime: '开始日期',
        endTime: '结束时间',
        status: '状态',
        date: '创建时间',
        time: '停留时间',
      },
      filename: '自定义表头.xlsx',
      json2sheetOpts: {
        // 指定顺序
        //header: ['id', 'no','name','avatar','address','beginTime','endTime','status','date','time'],
      },
    });
  }

  const actionColumn = reactive({
    width: 160,
    title: '操作',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render({ record }) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '删除',
            type: 'primary',
            status: 'danger',
            icon: renderIcon(DeleteOutlined),
            popConfirm: {
              content: '您真的，确定要删除吗？',
              okText: '确定',
              cancelText: '取消',
              onOk: handleDelete.bind(null, record),
            },
          },
          {
            label: '编辑',
            type: 'primary',
            icon: renderIcon(FormOutlined),
            popConfirm: {
              content: '您真的，确定要编辑吗？',
              okText: '确定',
              cancelText: '取消',
              onOk: handleEdit.bind(null, record),
            },
          },
        ],
      });
    },
  });

  function exportTabale() {
    if (!selectedRowsData.value.length) {
      return Message.error('请选择数据');
    }
    jsonToSheetXlsx({
      data: unref(selectedRowsData.value),
      header: {
        id: 'ID',
        no: '编码',
        name: '名称',
        avatar: '头像',
        address: '地址',
        beginTime: '开始日期',
        endTime: '结束时间',
        status: '状态',
        date: '创建时间',
        time: '停留时间',
      },
      filename: '选择表格数据.xlsx',
      json2sheetOpts: {
        // 指定顺序
        //header: ['id', 'no','name','avatar','address','beginTime','endTime','status','date','time'],
      },
    });
  }
</script>

<style lang="less" scoped></style>
